//
// Created by vcq on 21/4/6.
//

#pragma once

#include "dbng.hpp"
#include "utils_dao.hpp"
#include <string>

using namespace nlohmann;
using namespace std;
using Dao = dao_t<dbng < mysql>>;
struct _user_manager {
    int id;
    int user_id;
    int manager_id;
    string manager_type;
    string note;
};
REFLECTION(_user_manager, id, user_id, manager_id, manager_type, note)

json MGetUserManager(basic_string_view<char> id, basic_string_view<char> user_id,
                     basic_string_view<char> manager_id, basic_string_view<char> manager_type) {
    string sql = "select * from `_user_manager` where 1=1 ";
    if (!id.empty()) {
        sql += "and id =";
        sql += id;
    }
    if (!user_id.empty()) {
        sql += "and user_id =";
        sql += user_id;
    }
    if (!manager_id.empty()) {
        sql += "and manager_id =";
        sql += manager_id;
    }
    if (!manager_type.empty()) {
        sql += "and manager_id ='";
        sql += manager_type;
        sql += "'";
    }
    Dao dao;
    auto v = dao.query<_user_manager>(sql);
    nlohmann::json article_list, ret, item;
    for (auto &o : v) {
        item["id"] = o.id;
        item["user_id"] = o.user_id;
        item["manager_id"] = o.manager_id;
        item["manager_type"] = o.manager_type;
        item["note"] = o.note;
        article_list.push_back(item);
    }
    ret["code"] = 0;
    ret["count"] = article_list.size();
    ret["data"] = article_list;
    return ret;
}
void MAddUserManager(const struct _user_manager& _user_manager1){
    Dao dao;
    dao.update_object(_user_manager1);
}
void MDeleteUserManager(basic_string_view<char> id){
    MDeleteAll("_user_manager",id);
}